<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-5-11
 * Time: 下午4:49
 * 充电运营服务商控制器
 */
namespace Admin\Controller;
use Mcit\Page;

class ServicesController extends BaseController {

    //首页
    public function index(){
        $this->services = M("oper")->where(1)->select();
        $this->display();
    }

    //添加服务商
    public function addService(){
        if(IS_POST){
            $sqldata['oper_name']   = I("oper_name");
            $sqldata['tel']   = I("tel");
            $sqldata['creat_time'] = date("Y-m-d H:i:s",time());

            $map['oper_name']  = $sqldata['oper_name'];
            if(M("oper")->where($map)->find()){
                $return['state'] = 200;
                $return['msg']   = "您已添加了该服务商";
                echo json_encode($return);
                exit;
            }
            $result = M("oper")->add($sqldata);
            if($result){
                $return['oper_name']  = $sqldata['oper_name'];
                $return['id']    = $result;
                $return['url']   = U('Services/index');
                $return['state'] = 100;
                $return['msg']   = "添加成功";
            }else{
                $return['state'] = 200;
                $return['msg']   = "添加失败";
            }
            echo json_encode($return);
            exit;

        }
    }

    //修改服务商名称
    public function editServices(){

        $id         = I("id");
        $oper_name  = I("oper_name");
        $tel        = I("tel");
        $map['id']  = array('neq',$id);
        $map['oper_name']  = array('eq',$oper_name);

        if(M('oper')->where($map)->find()){
            $return['state'] = 200;
            $return['msg']   = "已有该服务商,服务商名称不能重复";
            echo json_encode($return);
            exit;
        }else{
            $sqldata['oper_name']  = $oper_name;
            $sqldata['tel']        = $tel;
            $saveMap['id']         = $id;
            $result = M("oper")->where($saveMap)->save($sqldata);
            if($result){
                $return['oper_name'] = $oper_name;
                $return['tel'] = $tel;
                $return['op']  = $oper_name;
                $return['state']  = 100;
                $return['msg']    = "保存成功";
            }else{
                $return['state']  = 200;
                $return['msg']    = "保存失败";
            }
            echo json_encode($return);
            exit;
        }
    }

    //服务商下城市管理
    public function scity(){
        $map['oper_id']    = I("id");
        $count = M("oper_city")->where($map)->count();
        //分页
        vendor('Mcit.Sys.Page');
        $Page = new Page($count, 20);
        $pageView = $Page->show();
        $this->assign('pageView', $pageView); //发送分页
        $nowPage = isset($_GET['page']) ? $_GET['page'] : 1;
        $scity = M("oper_city")->where($map)->page($nowPage . ',' . $Page->listRows)->select();
        //分页结束
        foreach($scity as $k => $v){
            if(mb_strlen($v['content'],"utf-8") > 10){
                $scity[$k]['content_text'] = mb_substr($v['content'],0,10,'utf-8')."...";
            }else{
                $scity[$k]['content_text'] = $v['content'];
            }
            $scity[$k]['oper_name'] = M("oper")->where("id='".$v['oper_id']."'")->getField("oper_name");
            $scity[$k]['oper_city'] = $v['oper_provinces'].'-'.$v['oper_city'];
            $way = strsToArray(rtrim($v['oper_pay_way'],','));
            if($way[0] == 1 && $way[1] == 2 && $way[2] == 3 && $way[3] == 4){
                $scity[$k]['oper_pay_way'] = "APP、卡、现金、其他";
            }elseif($way[0] == 1 && $way[1] == 2 && $way[2] == 3){
                $scity[$k]['oper_pay_way'] = "APP、卡、现金";
            }elseif($way[0] == 1 && $way[1] == 2){
                $scity[$k]['oper_pay_way'] = "APP、卡";
            }elseif($way[0] == 1 && $way[1] == 3){
                $scity[$k]['oper_pay_way'] = "APP、现金";
            }elseif($way[0] == 1 && $way[1] == 4){
                $scity[$k]['oper_pay_way'] = "APP、其他";
            }elseif($way[0] == 2 && $way[1] == 3){
                $scity[$k]['oper_pay_way'] = "刷卡、现金";
            }elseif($way[0] == 2 && $way[1] == 4){
                $scity[$k]['oper_pay_way'] = "刷卡、其他";
            }elseif($way[0] == 3 && $way[1] == 4){
                $scity[$k]['oper_pay_way'] = "现金、其他";
            }elseif($way[0] == 1){
                $scity[$k]['oper_pay_way'] = "APP";
            }elseif($way[0] == 2){
                $scity[$k]['oper_pay_way'] = "刷卡";
            }elseif($way[0] == 3){
                $scity[$k]['oper_pay_way'] = "现金";
            }elseif($way[0] == 4){
                $scity[$k]['oper_pay_way'] = "其他";
            }
        }
        $this->distribution = $scity;
        $this->display();
    }

    //分布城市详情
    public function detail(){
        $map['id'] = I("id");
        $cityOne   = M("oper_city")->where($map)->find();
        $cityOne['oper_name']   = M("oper")->where("id='".$cityOne['oper_id']."'")->getField("oper_name");
        $way = strsToArray(rtrim($cityOne['oper_pay_way'],','));
        if($way[0] == 1 && $way[1] == 2 && $way[2] == 3 && $way[3] == 4){
            $cityOne['oper_pay_way'] = "APP、卡、现金、其他";
        }elseif($way[0] == 1 && $way[1] == 2 && $way[2] == 3){
            $cityOne['oper_pay_way'] = "APP、卡、现金";
        }elseif($way[0] == 1 && $way[1] == 2){
            $cityOne['oper_pay_way'] = "APP、卡";
        }elseif($way[0] == 1 && $way[1] == 3){
            $cityOne['oper_pay_way'] = "APP、现金";
        }elseif($way[0] == 1 && $way[1] == 4){
            $cityOne['oper_pay_way'] = "APP、其他";
        }elseif($way[0] == 2 && $way[1] == 3){
            $cityOne['oper_pay_way'] = "刷卡、现金";
        }elseif($way[0] == 2 && $way[1] == 4){
            $cityOne['oper_pay_way'] = "刷卡、其他";
        }elseif($way[0] == 3 && $way[1] == 4){
            $cityOne['oper_pay_way'] = "现金、其他";
        }elseif($way[0] == 1){
            $cityOne['oper_pay_way'] = "APP";
        }elseif($way[0] == 2){
            $cityOne['oper_pay_way'] = "刷卡";
        }elseif($way[0] == 3){
            $cityOne['oper_pay_way'] = "现金";
        }elseif($way[0] == 4){
            $cityOne['oper_pay_way'] = "其他";
        }
        $this->cityOne = $cityOne;
        $this->display();
    }

    //添加城市
    public function addcity(){
        if(IS_POST){
            if(I("oper_city") == "市辖区" || I("oper_city") == "县"){
                $sqldata['oper_city']  = I("oper_provinces");
            }else{
                $sqldata['oper_city']  = I("oper_city");
            }
            $sqldata['oper_pay_way']   = I("oper_pay_way1").",".I("oper_pay_way2").",".I("oper_pay_way3").",".I("oper_pay_way4");
            $sqldata['oper_provinces'] = I("oper_provinces");
            $sqldata['oper_id']        = I("oper_id");
            $sqldata['content']        = I("content");
            $sqldata['oper_tel']       = I("oper_tel");
            $sqldata['creat_time']     = date("Y-m-d H:i:s",time());
            $map['oper_city']          = $sqldata['oper_city'];
            if(M("oper_city")->where($map)->find()){
                $return['state'] = 200;
                $return['msg']   = "您已添加了该城市信息!";
                echo json_encode($return);
                exit;
            }
            $result = M("oper_city")->add($sqldata);
            if($result){
                $return['url']   = U('Services/scity',array('id'=>$sqldata['oper_id']));
                $return['state'] = 100;
                $return['msg']   = "添加成功";
            }else{
                $return['state'] = 200;
                $return['msg']   = "添加失败";
            }
            echo json_encode($return);
            exit;
        }
        $this->services = M("oper")->where(1)->select();
        $this->china = D("Admin/Region")->getChinaArea();
        $this->display();
    }

    //修改分布城市信息
    public function editcity(){
        $map['id'] = I("id");
        $cityOne   = M("oper_city")->where($map)->find();
        $cityOne['oper_name'] = M("oper")->where("id='".$cityOne['oper_id']."'")->find();
        if(IS_POST){
            if(I("oper_city") == "市辖区" || I("oper_city") == "县"){
                $sqldata['oper_city']  = I("oper_provinces");
            }else{
                $sqldata['oper_city']  = I("oper_city");
            }
            $sqldata['oper_pay_way']   = I("oper_pay_way1").",".I("oper_pay_way2").",".I("oper_pay_way3").",".I("oper_pay_way4");
            $sqldata['oper_provinces'] = I("oper_provinces");
            $sqldata['oper_id']        = I("oper_id");
            $where['id']               = I("id");
            $sqldata['content']        = I("content");
            $sqldata['oper_tel']       = I("oper_tel");
            $result = M("oper_city")->where($where)->save($sqldata);
            if($result){
                $return['url']   = U('Services/scity',array('id'=>$sqldata['oper_id']));
                $return['state'] = 100;
                $return['msg']   = "添加成功";
            }else{
                $return['state'] = 200;
                $return['msg']   = "添加失败";
            }
            echo json_encode($return);
            exit;
        }
        $this->services = M("oper")->where(1)->select();
        $this->china = D("Admin/Region")->getChinaArea();
        $this->cityOne = $cityOne;
        $this->display();
    }

    //删除当前城市信息
    public function delCity(){
        $map['id'] = I("id");
        $result = M("oper_city")->where($map)->delete();
        if($result){
            $return['state'] = 100;
            $return['msg']   = "删除成功";
        }else{
            $return['state'] = 200;
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
    }

    //删除当前服务商及关联数据 谨慎操作
    public function delServices(){

        $id = I("id");
        $map['id']       = $id;
        $mapM['oper_id'] = $id;
        M('oper')->startTrans();//开启事务
        $hasMajor = M("oper_city")->where($mapM)->select();
        if(empty( $hasMajor ) ){
            $resultMajor = 1;
        }else{
            $resultMajor = M("oper_city")->where($mapM)->delete();
        }
        $result = M("oper")->where($map)->delete();
        if($result && $resultMajor){
            M('oper_city')->commit();//事务提交
            $return['state'] = 100;
            $return['msg']   = "删除成功";
        }else{
            M('oper')->rollback();//回滚
            $return['state'] = 200;
            $return['msg']   = "删除失败";
        }
        echo json_encode($return);
        exit;
    }
}